package com.asset.dispoal.company.service.commom;

import com.asset.dispoal.company.domain.db.entity.CompanyTempStatistics;
import com.asset.dispoal.company.domain.db.mapper.SysCompanyMapper;
import com.asset.dispoal.company.dto.CompanyAreaStatisticsRequest;
import com.asset.dispoal.company.dto.CompanyAreaStatisticsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.core.util.StringUtil;
import tech.mhuang.pacebox.springboot.protocol.Result;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class QueryCompanyStatisticsCommand implements BaseCommand<CompanyAreaStatisticsRequest, Result<List<CompanyAreaStatisticsResponse>>> {

    @Autowired
    private SysCompanyMapper companyMapper;

    public Result<List<CompanyAreaStatisticsResponse>> execute(CompanyAreaStatisticsRequest request) {
        List<CompanyTempStatistics> statisticsList = companyMapper.queryStatisticsMap(request.getType());
        if (CollectionUtil.isEmpty(statisticsList)) {
            return Result.ok();
        }
        Map<String, Set<String>> codeMap = new HashMap<>();
        //如果没有code则
        if (StringUtil.isEmpty(request.getCode())) {
            for (CompanyTempStatistics companyTempStatistics : statisticsList) {
                String code = companyTempStatistics.getCode().substring(0, 2);
                Set<String> nameSet = codeMap.getOrDefault(code, new HashSet<>());
                nameSet.add(companyTempStatistics.getName());
                codeMap.put(code, nameSet);
            }
        } else {
            for (CompanyTempStatistics companyTempStatistics : statisticsList) {
                if (companyTempStatistics.getCode().startsWith(request.getCode())) {
                    if (StringUtil.length(request.getCode()) == 2) {
                        String code = companyTempStatistics.getCode().substring(0, 4);
                        Set<String> nameSet = codeMap.getOrDefault(code, new HashSet<>());
                        nameSet.add(companyTempStatistics.getName());
                        codeMap.put(code, nameSet);
                    } else {
                        String code = companyTempStatistics.getCode();
                        Set<String> nameSet = codeMap.getOrDefault(code, new HashSet<>());
                        nameSet.add(companyTempStatistics.getName());
                        codeMap.put(code, nameSet);
                    }
                }
            }
        }
        List<CompanyAreaStatisticsResponse> responseList = new ArrayList<>();

        codeMap.forEach((code, data) -> {
            CompanyAreaStatisticsResponse response = new CompanyAreaStatisticsResponse();
            response.setCode(code);
            response.setNum(data.size());
            response.setNameList(new ArrayList<>(data));
            responseList.add(response);
        });
        return Result.ok(responseList);
    }
}